﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Configuration;
using System.IO;
using System.ServiceModel;

namespace GreetingWCFService
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            // Capatcha content.
            Dictionary<string, XDocument> captchaLetters = new Dictionary<string, XDocument>();
            string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            foreach (var letter in characters)
            {
                captchaLetters.Add(letter.ToString(), GetXAML(letter.ToString()));
            }
            captchaLetters.Add("line", GetXAML("line"));
            
            HttpContext.Current.Application.Add("CaptchaLetters", captchaLetters);
        }

        private static XDocument GetXAML(string letter)
        {
            string path = ConfigurationManager.AppSettings.Get("letterPath") + letter.ToString() + ".xaml";

            FileStream fs = null;
            StreamReader sr = null;
            StringReader str = null;

            try
            {
                fs = File.Open(path, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);
                return XDocument.Parse(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

                if (str != null) str.Close();
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}